package es.franciscovargas.buscalabs;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.SimpleCursorAdapter;

public class ListaLabos extends ListActivity {

	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		DataBaseHelper dbh = new DataBaseHelper(this, "horario", null, 1);
		SQLiteDatabase db = dbh.getWritableDatabase();

		final GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTimeZone(TimeZone.getTimeZone("Europe/Madrid"));
		final int diasemana = calendar.get(Calendar.DAY_OF_WEEK);
		final int hora = calendar.get(Calendar.HOUR_OF_DAY);

		Cursor c = db.rawQuery(
				"SELECT lab , hasta, _id FROM puestos WHERE dia = "
						+ diasemana + " AND libre = 1 AND hora = " + hora
						+ " GROUP BY lab ORDER BY hasta DESC", null);

		String[] from = new String[] { "lab", "hasta", "_id" };
		int[] to = new int[] { R.id.label, R.id.tiempolibre };
		SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
				R.layout.rowlayout, c, from, to);
		this.setListAdapter(mAdapter);

		db.close();
	}
}
